package com.hp.his.basic.web;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.hp.his.basic.entity.ExamSummary;
import com.hp.his.basic.entity.UPMSIssue;
import com.hp.his.basic.entity.User;
import com.hp.his.basic.service.UPMSBaseManager;
import com.hp.his.core.util.web.struts2.CrudActionSupport;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/exam")
@Results(
		{  @Result(name = "home", location = "index.action", type = "redirect")  }
)
public class LoginAction extends  CrudActionSupport<User> {
	
	private String userName = "";
	private String password = "";
	
	@Autowired
	private UPMSBaseManager baseManager;
	
	public String logoff() throws Exception {
		ActionContext context = ActionContext.getContext();
		Map session = context.getSession();  
		session.clear();
		return "home";		
	}
	
	public String clearRequestedURL() throws Exception{
		ActionContext context = ActionContext.getContext();
		Map session = context.getSession();  
		session.remove("RequestedURL");
		
		return null;
	}

	public String submit() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse(); 
		response.setCharacterEncoding("UTF-8");      
		response.setContentType("text/plain");  
		PrintWriter out = response.getWriter();  
		
		String jasonStr = "";

		if(userName !=null && userName.trim().length()>0){
			User user = baseManager.getUserByName(userName.trim());
			if(user == null ){
				jasonStr = "{'status':-1}";
			}else if(!user.getPassword().trim().equals(password)){
				jasonStr = "{'status':0}";
			}else{
				//登录成功
				ActionContext context = ActionContext.getContext();
				Map session = context.getSession();  
				
				jasonStr = "{'status':1, 'userName':'" +user.getUserName()+"'";
				if(session.get("RequestedURL")!=null){
					jasonStr = jasonStr + ", 'requestedURL':'"+session.get("RequestedURL")+"'}";
				}else{
					jasonStr = jasonStr + "}";
				}
								
				session.clear();
				session.put("loggedUser", user);
			}
		}
		
		out.println(jasonStr);   
		out.flush();
		out.close();
		
		return null;
		
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public User getModel() {
		return null;
	}

	@Override
	public String list() throws Exception {
		return "SUCCESS";
	}

	@Override
	public String input() throws Exception {
		return null;
	}

	@Override
	public String save() throws Exception {
		return null;
	}

	@Override
	public String delete() throws Exception {
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {		
	}



	
}
